import MapView from "@arcgis/core/views/MapView"; //视图
import Query from '@arcgis/core/tasks/support/Query';
import QueryTask from "@arcgis/core/tasks/QueryTask";
import { openLoading, closeLoading } from '../func/loading';
import { SymbolUtils } from "../../../utils/symbol-utils";
import { Geometry } from '@arcgis/core/geometry';
import { ElNotification } from "element-plus";
export class QueryTaskSearch {
    private _mapView: MapView;
    private _searchWhere: string;
    private _layerURL: string;

    /**
     * 根据条件查询图层信息的类
     * @param mapView 地图的view对象
     * @param layerURL 服务地址    
     * @param searchWhere 查询组合条件where语句
     */
    public constructor(mapView: MapView, layerURL: string, searchWhere: string) {
        this._mapView = mapView;
        this._searchWhere = searchWhere;
        this._layerURL = layerURL;
    }
    /**
     * 属性查询，并返回查询结果集
     * @returns 查询结果集Array
     */
    public QueryTaskResult<T>():Promise<Array<T>> {        
        openLoading();
        const list=new Array<T>();
        this.cleargraphics();
        //通过Query方式查询，创建查询参数
        const queryParameters = new Query({
            where: this._searchWhere,
            outFields: ["*"],
            returnGeometry: true
        })
        const queryTask = new QueryTask({
            url: this._layerURL
        });
        
        return new Promise((reslover, reject) => {
            queryTask
                .execute(queryParameters)
                .then(response => {
                    const resfeatures = response.features;
                    if (resfeatures.length > 0) {
                   
                        for (let index = 0; index < resfeatures.length; index++) {
                            let graphic;
                            //获得图形
                            graphic = resfeatures[index];
                            graphic.symbol = SymbolUtils.getLineSymbol();
                            // 将属性集合添加到集合中，返回
                            list.push(graphic.attributes);      
                            this._mapView.graphics.add(graphic);
                        }                        
                        closeLoading();
                        reslover(list);
                    } else {
                        closeLoading();
                        this.openNoticel("没有查询结果");
                    }
                })
                .catch((ex) => {
                    closeLoading();                                        
                    this.openNoticel("服务出现错误" + ex);
                })
        })
    }
    /**
     *异步 空间查询，并返回查询属性结果集
     * @param relationship 图形的查询方式：相交或包含
     * @param geometry map上的图形geoetry
     * @returns 查询结果集Array
     */
    public queryGraphic<T>(relationship: string, geometry: Geometry):Promise<Array<T>> {
        openLoading();
        const list=new Array<T>();
        const queryTask = new QueryTask({
            url: this._layerURL
        });
        //通过Query方式查询，创建查询参数
        const queryParameters = new Query({
            geometry: geometry,
            outFields: ["*"],
            returnGeometry: true,
            outSpatialReference: this._mapView.spatialReference,
            spatialRelationship: relationship,
        })

        return new Promise((reslover, reject) => {
            queryTask
                .execute(queryParameters)
                .then(response => {
                    const resfeatures = response.features;
                    if (resfeatures.length > 0) {
                        for (let index = 0; index<resfeatures.length; index++) {
                            let graphic;
                            //获得图形
                            graphic = resfeatures[index];
                            graphic.symbol = SymbolUtils.getLineSymbol();                                              
                            this._mapView.graphics.add(graphic);
                            list.push(graphic.attributes);
                        }
                        closeLoading();
                        reslover(list);
                    } else {
                        this.openNoticel('没有查询到信息');
                        closeLoading();
                        //reslover(list);
                    }
                    closeLoading();
                })
                .catch((res) => {
                    closeLoading();
                    console.log("异常。。。。。。。。。",res)       
                    this.openNoticel(res);
                })
        })
    }
    //警告提醒弹窗
    private openNoticel(warMeg: string) {
        ElNotification.error({
            title: "警告",
            message: warMeg,
            position: "bottom-right",
            offset: 200,
        });
    };

    // 清除地图上的graphics
    private cleargraphics() {
        this._mapView.graphics.removeAll();
    }
}